package cn.duxiaod.demo.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁测试
 */
public class LockMain {

    public static void main(String[] args) {
        Window window = new Window();
        for (int i = 0; i < 2; i++) {
            new Thread(window).start();
        }
    }
}
class Window implements Runnable{
    private volatile int num = 100;
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();
            try {
                if(num>0){
                    System.out.println(Thread.currentThread()+"窗口正在售票，票号为"+num);
                    num--;
                }else {
                    break;
                }
            }finally {
                lock.unlock();
            }
        }
    }
}
